package com.ydj.app.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ydj.app.entity.WebUser;
import com.ydj.app.protocol.http.cms.CmsCommonUploadFileRes;
import com.ydj.app.protocol.http.web.*;
import com.ydj.app.service.CommonService;
import com.ydj.framework.annotation.AllowGuest;
import com.ydj.framework.exception.CommonException;
import com.ydj.framework.filter.ExtHttpServletRequest;
import com.ydj.framework.protocol.BaseEnum;
import com.ydj.framework.protocol.JsonResult;
import com.ydj.framework.third.oss.OssPath;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/web/common")
@Api(tags = "WEB-公用")
public class WebCommonController {
    @Autowired
    CommonService commonService;

    @AllowGuest
    @ResponseBody
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "微信登录")
    @PostMapping(value = "/wxLogin")
    public JsonResult<WebCommonWxLoginRes> wxLogin(@RequestBody @Validated WebCommonWxLoginReq req) throws Exception {
        WebCommonWxLoginRes res = commonService.webWxLogin(req);
        return JsonResult.success(res);
    }

    @ResponseBody
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "微信登录后绑定手机或邮箱")
    @PostMapping(value = "/bindMobileEmail")
    public JsonResult<?> bindMobileEmail(@RequestBody @Validated WebCommonBindMobileEmailReq req, WebUser webUser) throws Exception {
        commonService.webBindMobileEmail(req, webUser);
        return JsonResult.success();
    }

    @AllowGuest
    @ResponseBody
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "邮箱登录")
    @PostMapping(value = "/emailLogin")
    public JsonResult<WebCommonEmailLoginRes> emailLogin(@RequestBody @Validated WebCommonEmailLoginReq req) throws Exception {
        WebCommonEmailLoginRes res = commonService.webEmailLogin(req);
        return JsonResult.success(res);
    }

    @AllowGuest
    @ResponseBody
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "获取邮箱验证码")
    @PostMapping(value = "/sendEmailVerifyCode")
    public JsonResult<?> sendEmailVerifyCode(@RequestBody @Validated WebCommonSendEmailVerityCodeReq req) throws Exception {
        commonService.webSendEmailVerifyCode(req);
        return JsonResult.success();
    }

    @AllowGuest
    @ResponseBody
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "手机登录")
    @PostMapping(value = "/mobileLogin")
    public JsonResult<WebCommonMobileLoginRes> mobileLogin(@RequestBody @Validated WebCommonMobileLoginReq req) throws Exception {
        WebCommonMobileLoginRes res = commonService.webMobileLogin(req);
        return JsonResult.success(res);
    }

    @AllowGuest
    @ResponseBody
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "获取手机号码验证码")
    @PostMapping(value = "/sendMobileVerifyCode")
    public JsonResult<?> sendMobileVerifyCode(@RequestBody @Validated WebCommonSendMobileVerityCodeReq req) throws Exception {
        commonService.webSendMobileVerifyCode(req);
        return JsonResult.success();
    }

    @ResponseBody
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "微信端获取手机号码")
    @PostMapping(value = "/getMobileByWxCode")
    @ApiIgnore
    public JsonResult<WebCommonGetMobileByWxCodeRes> getMobileByWxCode(@RequestBody @Validated WebCommonGetMobileByWxCodeReq req, @ApiIgnore WebUser webUser) throws Exception {
        WebCommonGetMobileByWxCodeRes res = commonService.webGetMobileByWxCode(req, webUser);
        return JsonResult.success(res);
    }

    @ResponseBody
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "获取账号信息")
    @PostMapping(value = "/getProfile")
    public JsonResult<WebCommonGetProfileRes> getProfile(@ApiIgnore WebUser webUser) throws CommonException {
        WebCommonGetProfileRes res = commonService.webGetProfile(webUser);
        return JsonResult.success(res);
    }

    @ResponseBody
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "上传文件")
    @PostMapping(value = "/uploadFile")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cosPath", value = "路径的枚举(Banner, 客服二维码)", required = true, dataType = "string"),
            @ApiImplicitParam(name = "fileName", value = "文件名", required = true, dataType = "string"),
            @ApiImplicitParam(name = "uploadFile", value = "上传文件", required = true, dataType = "uploadFile")
    })
    public JsonResult<?> uploadFile(@ApiIgnore ExtHttpServletRequest request, @ApiIgnore WebUser webUser) throws IOException, CommonException {
        String ossPathString = request.getParameter("cosPath");
        String fileName = request.getParameter("fileName");
        OssPath cosPath = BaseEnum.getByText(OssPath.values(), ossPathString);
        CommonException.throwExceptionIfNull(cosPath, "路径枚举错误");

        InputStream uploadFileInputStream = request.getUploadFileStream("uploadFile");
        String uploadFileUrl = commonService.uploadFile(uploadFileInputStream, fileName, cosPath);
        uploadFileInputStream.close();

        WebCommonUploadFileRes res = new WebCommonUploadFileRes()
                .setSuccess(uploadFileUrl != null)
                .setUrl(uploadFileUrl);
        return JsonResult.success(res);
    }
}